package com.wangcang.widgetlibrary.activity.base;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;

import com.iflytek.cloud.SpeechUtility;
import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.bean.AddressBean;
import com.wangcang.widgetlibrary.db.LibraryDbUtils;
import com.wangcang.widgetlibrary.tools.FileUtils;
import com.wangcang.widgetlibrary.tools.SharedPreferencesHelper;
import com.wangcang.widgetlibrary.tools.ThreadUtils;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;


/**
 * Created by jack.dong on 2017/5/31.
 */

public class MobileApplication extends Application {

    static MobileApplication instance = null;
    List<AddressBean> list;
    public static SharedPreferencesHelper sp = null;

    public static final int READ_ADDRESS_IN_JSON = 3;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what) {
                case READ_ADDRESS_IN_JSON:
                    String json = (String) msg.obj;
                    //得到地址的实体类对象
                    try {
                        JSONObject object = new JSONObject(json);
                        JSONArray array = object.getJSONArray("data");
                        list = new ArrayList<>();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject jsonObject = (JSONObject) array.get(i);
                            AddressBean bean = new AddressBean();
                            bean.setPARENT_AREA_ID(jsonObject.optString("parentAreaId"));
                            bean.setAREA_LEVEL(jsonObject.optString("areaType"));
                            bean.setAREA_NAME(jsonObject.optString("areaName"));
                            bean.setAREA_ID(jsonObject.optString("areaId"));
                            list.add(bean);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    ThreadUtils.newThread(new Runnable() {
                        @Override
                        public void run() {
                            LibraryDbUtils.deleteAllCity(MobileApplication.this);
                            LibraryDbUtils.saveCitys(list, MobileApplication.this);

                        }
                    });

                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        NoHttp.initialize(this);
        getAddressJson();
        sp = new SharedPreferencesHelper(this);
        SpeechUtility.createUtility(this, "appid=" + getString(R.string.app_id));


        //ShareSDK初始化
        ShareSDK.initSDK(this);
    }

    public static MobileApplication getApplicationInstance() {
        return instance;
    }

    private void getAddressJson() {
        //线程池读取本地json文件
        ThreadUtils.newThread(new Runnable() {
            @Override
            public void run() {
                String jsonStr = FileUtils.getJson(getApplicationContext(), "address.json");
                Message msg = new Message();
                msg.what = READ_ADDRESS_IN_JSON;
                msg.obj = jsonStr;
                mHandler.sendMessage(msg);
            }
        });
    }

    // 获取版本名
    public String getVersionName() {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo;
        String versionName = "";
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

}
